<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-caret-right"></i> 个人中心</el-breadcrumb-item>
      <el-breadcrumb-item>个人信息</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
      <el-form-item label="昵称" prop="nickName" >
        <el-input v-model="ruleForm.nickName"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-select style="height: 32px; width: 19%;" v-model="ruleForm.gender" placeholder="请选择性别">
          <el-option
              v-for="item in genderOptions"
              :key="item.key"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="身份" prop="status">
        <el-select style="height: 32px; width: 19%;" v-model="ruleForm.status" placeholder="请选择性别">
          <el-option
              v-for="item in statusOptions"
              :key="item.key"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="ruleForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="ruleForm.email"></el-input>
      </el-form-item>
      <el-form-item label="居住状态" prop="dwellingState">
        <el-select style="height: 32px; width: 19%;" v-model="ruleForm.dwellingState" placeholder="请选择性别">
          <el-option
              v-for="item in dwellingStateOptions"
              :key="item.key"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="兴趣爱好" prop="hobbies">
        <el-input v-model="ruleForm.hobbies"></el-input>
      </el-form-item>
      <el-form-item label="自我介绍" prop="selfIntroduction">
        <el-input v-model="ruleForm.selfIntroduction"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      genderOptions:[{key:'0',value:'男'},{key:'1',value:'女'}],
      statusOptions:[{key:'0',value:'在校学生'},{key:'1',value:'上班族'},{key:'2',value:'教师'},{key:'3',value:'自由职业'}],
      dwellingStateOptions:[{key:'0',value:'独居'},{key:'1',value:'和室友'},{key:'2',value:'和父母'},{key:'3',value:'自由职业'},{key:'4',value:'和孩子'},{key:'5',value:'和宠物'}],
      ruleForm: {
        nickName:'',
        gender:'',
        status:'',
        phone:'',
        email:'',
        dwellingState:'',
        hobbies:'',
        selfIntroduction:''
      },
      rules: {
        nickName: [
          {required: true, message: '请输入昵称', trigger: 'blur'},
          {min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur'}
        ],
        phone: [
          {required: true, message: '请输入手机号', trigger: 'blur'},
          {min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur'}
        ],
      }
    };
  },
  methods: {
    loadUserInfo() {
      let url = 'http://localhost:8080/user/standard';
      this.axios.post(url).then((response) => {
        this.ruleForm=response.data.data;
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:8080/user/submit';
          console.log('url = ' + url);
          let formData = this.qs.stringify(this.ruleForm, {'arrayFormat': 'repeat'});
          console.log('formData = ' + formData);
          this.axios.post(url, formData).then((response) => {
            console.log('服务器端响应的结果：');
            console.log(response);
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              this.$message({
                message: '添加成功！',
                type: 'success'
              });
              this.loadUserInfo(formName);
            } else {
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.loadUserInfo();
  }
}
</script>

<style scoped>

</style>